package com.bak.tt.manager;

import com.bak.tt.model.Account;
import com.bak.tt.utils.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by yingsy on 2021-01-26.
 */
public class AccountManager {

    public Account queryAccountByCardNo(String cardNo) throws SQLException, ClassNotFoundException {
        Connection connection = DruidUtils.getConnection();
        String sql = "select * from account where card_no=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();
        Account account = new Account();
        while (resultSet.next()){
            account.setCardNo(resultSet.getString("card_no"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return account;
    }

    public void updateAccountByCardNo(Account account) throws SQLException, ClassNotFoundException {
        Connection connection = DruidUtils.getConnection();
        String sql = "update account set money=? where card_no=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,account.getMoney());
        preparedStatement.setString(2,account.getCardNo());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }
}
